package com.zrf.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import com.zrf.pojo.Flow;

@Repository("flowDao")
public class FlowDaoImpl extends BaseDaoImpl<Flow>{

	private Session session = null;
	private Query query = null;
	public void add(Flow f) {
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			Transaction tr = session.beginTransaction();
			session.save(f);
			tr.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (session!=null&&session.isOpen()) {
			session.close();
		}
	}
	public void update(Flow f) {
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			Transaction tr = session.beginTransaction();
			session.update(f);
			tr.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (session!=null&&session.isOpen()) {
			session.close();
		}
	}
	public List<?> findByState(String name, Integer state) {
		// TODO Auto-generated method stub
		List<?> list = null;
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery("FROM Flow f WHERE f.fstate=? AND f.person.pname=?");
			query.setInteger(0, state);
			query.setString(1, name);
			list = query.list();
			if (list!=null&&list.size()<1) {
				list = null;
			}
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (session!=null&&session.isOpen()) {
			session.close();
		}
		return list;
	}
	public List<?> findByNoState(String name, Integer noState) {
		// TODO Auto-generated method stub
		List<?> list = null;
		try {
			session = this.getHibernateTemplate().getSessionFactory().openSession();
			query = session.createQuery("FROM Flow f WHERE f.fstate!=? AND f.person.pname=?");
			query.setInteger(0, noState);
			query.setString(1, name);
			list = query.list();
			if (list!=null&&list.size()<1) {
				list = null;
			}
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (session!=null&&session.isOpen()) {
			session.close();
		}
		return list;
	}
}
